class SiteMainController < ApplicationController

  def index
  end

  def stats
    @players = Player.count
    @kills = Kill.count
    @games = Game.count
  end

  def rules
  end

  def change_image

  end

  def crop
    @new_player = Player.find(params[:id])
    if @new_player
      if request.post?
        # we got a post request, so first see if the cancel button was clicked
        if params[:crop_cancel] && params[:crop_cancel] == "true"
          # this means the cancel button was clicked. you might
          # want to implement a more-sophisticated cancel behavior
          # in your app -- for instance, if you store the previous
          # request in the session, you could redirect there instead
          # of to the app's root, as i'm doing here.
          flash[:notice] = "Cropping canceled."
          go_back
          return
        end
        # cancel was not clicked, so crop the image
        @new_player.crop! params
        if @new_player.save
          flash[:notice] = "Image cropped and saved successfully."
          session[:image_chosen?] = true
          go_back
          return
        end
      end
    else
      flash[:error] = "Nothing to crop."
      go_back
    end
  rescue Player::InvalidCropRect
    flash[:error] = "Sorry, could not crop the image."
  end
  
  def reset
    reset_session
    redirect_to :back
  end
  
  
  private
  
  def go_back
    back = session[:crop_referrer]
    session[:crop_referrer] = nil
    redirect_to back
  end
  
end
